<style type="text/css">
.text-c td img {
	width: 48px;
	height:48px;
}</style>

<link href="__PUBLIC__/css/page.css" rel="stylesheet"  type="text/css" />

<div class="pd-20">
    <if condition="$users eq null ">
        <div class="row">没有订单</div>
        <else />
        <div class="mt-20">
	        <div style="padding-bottom: 10px;">
		        <form method="POST" action="{:U('Admin/Buyer/showUsers')}">
	        	<a class="btn btn-success" onclick="synchWXUser()" id="synchBtn" onmouseover="showSynchBtnTips(this)">同步微信用户</a>&nbsp;&nbsp;
	<a class="btn btn-success"  onclick="add_edit_User('新增用户组','{:U("Admin/Buyer/addUser")}','4','500',400)">添加用户</a>
				<div style="display: inline;float: right;">
				<span style="padding-left: 5px;">关键词：</span>
				<input type="text" class="input-text" id="keyword" name="keyword" style="width: 200px;" value="" placeholder="请输入关键字...">
				<input type="submit" class="btn btn-success" value="搜索"/>
				</div>
				</form>
	        </div>
            <table class="table table-border table-bordered table-bg table-sort table-striped">
                <thead>
                    <tr class="text-c">
                        <th width="20">编号</th>
                        <th width="30">昵称</th>
                        <th width="50">头像</th>
                        <th width="30">真名</th>
                        <th width="25">菜篮子号</th>
                        <th width="20">套餐金额</th>
                        <th width="250">地址</th>
                        <th width="50">操作</th>
                    </tr>
                </thead>
                <!--{$vo.postcode}-->
                <tbody>
	                <php> $count=0; </php>
                    <volist name="users" id="user" key="k">
                    	<php> $count++; </php>
                        <tr class="text-c">
                            <td>{$user.id}</td>
                            <td class="nickname">{$user.nickname}</td>
                            <td class="headimg"><img src="{$user.headimgurl}"></td>
                            <td>{$user.truename}</td>
                            <td>{$user.basketid}</td>
                            <td>{$user.discount_money}</td>
                            <td>{$user.address}</td>
                            <td>
                            	<a class="btn btn-success" id="btn-tips-{$count}" onclick="add_edit_User('修改用户', '{:U("Admin/Buyer/editUser", array("id" => $user["id"]))}', '4','500',420)" onmouseover="showBndBtnTips(this)">绑定用户</a>
                            	<a class="btn btn-success" onclick="update_user(this,'{$user.openid}')">更新用户</a>
                            </td>
                            <!--td><a class="btn btn-success" onclick="manage_edit('用户修改','{:U(" Vip1/userEdit ",array("openid "=>$vo["openid "]))}','4','','450', 'aaa')">修改</a></td-->
                        </tr>
                    </volist>
                </tbody>
            </table>
        </div>
    </if>
    <div class="row cl dataTables_wrapper" id="upage">
        {$Page}
    </div>
</div>
<script type="text/javascript" src="__PUBLIC__/lib/layer/layer.js"></script>
<script>
// 用户编辑
function add_edit_User(title, url, id, w, h) {
    layer_show(title, url, w, h);
}

// 更新用户微信信息
function update_user(id, openid) {
	$.ajax({
		url: '{:U("Admin/Buyer/updateUserInfo")}',
		type: 'POST',
		asyn:true,
		data:{
			id:openid
		},
		timeout: 10000,
		dataType:'json',
		success:function(data, textStatus, jsqXHR) {
			if(data['headimgurl'] == null) {
				alert(data);
			}else {
				var collocations = eval(data);
				var parent = $(id).parent().parent();
				parent.children('.nickname').text(data['nickname']);
				parent.children('.headimg').children('img').attr('src', data['headimgurl']);
			}
		},
		error:function(data, textStatus) {
			alert("获取用户资料出错或用户已经取消关注");
		},
	});
}

function showBndBtnTips(btn) {
	var id = $(btn).attr('id');
	layer.tips('将微信用户与签约用户绑定', '#'+id, {
	  tips: [4, '#005AAB']
	});
}

function showSynchBtnTips(btn) {
	var id = $(btn).attr('id');
	
	layer.tips('用户多，更新较慢<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请稍等!', '#'+id, {
	  tips: [3, '#005AAB']
	});
}

function synchWXUser() {
	var index = layer.load(0);
	$.ajax({
		url: '{:U("Admin/Buyer/synchWXUser")}',
		type: 'POST',
		asyn: true,
		data: {},
		timeout: 100000,
		dataType: 'json',
		success: function(data, textStatus, jsqXHR) {
			if(data.code == 0) {
				layer.close(index); 	// 关闭加载tips
				window.location.href='{:U("Admin/Buyer/showUsers")}';
			}
		}
	});
}

$(document).ready(function(){
	// 处理分页链接，加入keyword，注意和thinkphp url模式适配
	$(".pager").find(".num").each(function(){
		var url = $(this).attr("href");
		if(url.indexOf("/keyword") == -1) {
			url = url + '/keyword/' + '{$keyword}';
			$(this).attr("href", url);
		}
	});
});
</script>
